﻿using System;
using System.Data;
using System.Data.SqlClient;
using Dtl;
using System.Collections.Generic;
using System.Collections;

namespace Dal
{
    public class ScheduleDao
    {

        public static DataSet getPeriodSchedule()
        {

            string cnx = Credentials.getCredentias("DMSTools"); 
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_get_PeriodSchedule";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtPeriodSchedule");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getPeriodSchedule: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsPeriodSchedule");
            ds.Tables.Add(dt);
            return ds;
        }

        public static DataSet getPeriodScheduleAlerts()
        {

            string cnx = Credentials.getCredentias("DMSTools");
            if (cnx == null) return null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(cnx);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dms_Get_OnlyScheduleAlerts";
            cmd.CommandTimeout = 0;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("dtScheduleAlerts");
            try
            {
                cmd.Connection.Open();
                da.Fill(dt);
                cmd.Connection.Close();
            }
            catch (Exception ex)
            {
                Credentials.writeLogFile("getPeriodScheduleAlerts: " + ex.Message);
                return null;
            }
            DataSet ds = new DataSet("dsScheduleAlerts");
            ds.Tables.Add(dt);
            return ds;
        }

        public static int setScheduleAlerts(List<Schedule> listSchedule)
        {
            int insert = 0;

            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }

            SqlTransaction tr = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);

            try
            {
                cmd.Connection.Open();
                cmd.Connection.CreateCommand();
                tr = cmd.Connection.BeginTransaction("Transaction");
                cmd.Transaction = tr;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                foreach (Dtl.Schedule schedule in listSchedule)
                {
                    if (schedule.typeOperation == true) //GUARDAR UNA ALERTA
                    {
                        cmd.CommandText = "dbo.dms_Set_ScheduleAlerts";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@NextTimeSchedule", schedule.NextTimeSchedule);
                        cmd.Parameters.AddWithValue("@LastTimeExecSchedule", schedule.LastTimeExecSchedule);
                        cmd.Parameters.AddWithValue("@IntervalSchedule", schedule.IntervalSchedule);
                        cmd.Parameters.AddWithValue("@periodSchedule", schedule.periodSchedule);
                        cmd.Parameters.AddWithValue("@idEmailGroup", schedule.idEmailGroup);
                        

                    }
                    else //MODIFICA UNA ALERTA
                    {
                        cmd.CommandText = "dbo.dms_Set_ScheduleAlerts_Change";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@idSchedule", schedule.idScheduler);
                        cmd.Parameters.AddWithValue("@NextTimeSchedule", schedule.NextTimeSchedule);
                        cmd.Parameters.AddWithValue("@IntervalSchedule", schedule.IntervalSchedule);
                        cmd.Parameters.AddWithValue("@periodSchedule", schedule.periodSchedule);
                        cmd.Parameters.AddWithValue("@idEmailGroup", schedule.idEmailGroup);

                    }

                    insert = cmd.ExecuteNonQuery();

                }
                 
                tr.Commit();
                cmd.Connection.Close();
                return insert;
            }
            catch (Exception exc)
            {
                tr.Rollback();
                cmd.Connection.Close();
                Credentials.writeLogFile("setScheduleAlerts+: " + exc.Message);
                return -2;
            }

        }

        public static int DeleteSchedule(int idSchedule)
        {
            int delete = 0;
            string connection = Credentials.getCredentias("DMSTools");
            if (connection == null)
            {
                return 0;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connection);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = "dbo.dms_delete_ScheduleAlerts";
            cmd.Parameters.AddWithValue("@idSchedule", idSchedule);


            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                delete = 1;
                return delete;
            }
            catch (SqlException exc)
            {

                Credentials.writeLogFile("DeleteSchedule: " + exc.Message);
                return 0;
            }


        }
    }
}
